package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.core.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<BaseCategory1> findCategory1IdList() {
		return baseCategory1Mapper.selectList(null);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//	根据一级分类Id查询二级分类集合
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
		//	获取到二级分类Id
		List<Long> category2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		//	根据二级分类Id 集合获取三级分类数据 select * from base_category3 where  category2_id in (101,102,103) and is_top = 1 limit 7;
		return  this.baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().in(BaseCategory3::getCategory2Id,category2IdList).eq(BaseCategory3::getIsTop,1).last(" limit 7 "));
	}

	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		//	创建一级分类对象
		JSONObject category1 = new JSONObject();
		// select * from base_category_view where category1_id = 1;
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
		//	添加数据
		category1.put("categoryId",category1Id);
		category1.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());
		//	封装一级分类下的二级分类集合数据;
		Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		//	循环遍历
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = category2Map.entrySet().iterator();
		//	创建一个二级分类对象集合
		ArrayList<JSONObject> categoryChild2List = new ArrayList<>();
		while (iterator.hasNext()){
			//	获取entry对象
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			Long category2Id = entry.getKey();
			List<BaseCategoryView> baseCategoryViewList1 = entry.getValue();
			//	创建二级分类对象
			JSONObject category2 = new JSONObject();
			category2.put("categoryId",category2Id);
			category2.put("categoryName",baseCategoryViewList1.get(0).getCategory2Name());
			//	获取三级分类数据添加到二级分类中
			List<JSONObject> list = baseCategoryViewList1.stream().map(baseCategoryView -> {
				//	创建三级分类对象
				JSONObject category3 = new JSONObject();
				category3.put("categoryId", baseCategoryView.getCategory3Id());
				category3.put("categoryName", baseCategoryView.getCategory3Name());
				return category3;
			}).collect(Collectors.toList());
			category2.put("categoryChild",list);

			//	将二级分类对象添加到集合中
			categoryChild2List.add(category2);
		}
		//	将二级分类集合添加到一级分类中
		category1.put("categoryChild",categoryChild2List);
		//	返回数据
		return category1;
	}

	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		//	调用mapper 方法
		return baseAttributeMapper.selectAttribute(category1Id);
	}

	@Override
	public List<JSONObject> getBaseCategoryList() {
		//	构建一级分类的Json 字符串集合！
		List<JSONObject> list = new ArrayList<>();
		//	利用视图查询数据：select * from base_category_view where is_deleted = 0;
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//	以 一级分类Id为key List<BaseCategoryView> 为value;
		Map<Long, List<BaseCategoryView>> categoryMap = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//	循环遍历这个集合
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = categoryMap.entrySet().iterator();
		while (iterator.hasNext()){
			//	获取下一条数据
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			Long category1Id = entry.getKey();
			List<BaseCategoryView> baseCategoryViewList1 = entry.getValue();
			//	创建一个一级分类对象 要想给这个对象添加数据，只需要调用put();方法
			JSONObject category1 = new JSONObject();
			category1.put("categoryId", category1Id);
			category1.put("categoryName",baseCategoryViewList1.get(0).getCategory1Name());
			//	获取到二级分类集合
			//	以二级分类Id 分组获取数据
			Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViewList1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//	遍历map 集合
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = category2Map.entrySet().iterator();
			//	创建二级分类集合对象
			ArrayList<JSONObject> categoryChild2List = new ArrayList<>();
			while (iterator1.hasNext()){
				//	获取二级分类Id
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				Long category2Id = entry1.getKey();
				List<BaseCategoryView> baseCategoryViewList2 = entry1.getValue();
				//	创建一个二级分类对象
				JSONObject category2 = new JSONObject();
				category2.put("categoryId", category2Id);
				category2.put("categoryName",baseCategoryViewList2.get(0).getCategory2Name());
				//	创建二级分类集合对象
				//				ArrayList<JSONObject> categoryChild3List = new ArrayList<>();
				//				//	要获取三级分类集合并赋值给二级对象
				//				baseCategoryViewList2.forEach(baseCategoryView -> {
				//					//	创建三级分类对象
				//					JSONObject category3 = new JSONObject();
				//					category3.put("categoryId",baseCategoryView.getCategory3Id());
				//					category3.put("categoryName",baseCategoryView.getCategory3Name());
				//					categoryChild3List.add(category3);
				//				});
				//				category2.put("categoryChild",categoryChild3List);
				//	循环遍历这个集合，获取集合中的每个数据，并将数据添加一个集合中！
				List<JSONObject> categoryChild3List = baseCategoryViewList2.stream().map(baseCategoryView -> {
					//	创建三级分类对象
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					//	返回三级分类对象
					return category3;
				}).collect(Collectors.toList());
				//	将三级分类数据添加到二级对象中！
				category2.put("categoryChild",categoryChild3List);
				//	添加二级对象到集合列表
				categoryChild2List.add(category2);
			}
			//	将二级分类集合添加到一级分类中.
			category1.put("categoryChild",categoryChild2List);
			//	将一级分类对象添加到集合中
			list.add(category1);
		}

		//	返回数据
		return list;
	}
}
